package com.blog.service;

import cn.hutool.core.bean.BeanUtil;
import com.blog.dao.PostDao;
import com.blog.entity.Post;
import com.blog.utils.BaseService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

/**
 * @author: chengqiang
 * @date: 2018-12-06 21:59:35
 */
@Service
public class PostService extends BaseService<Post, PostDao> {

    /**
     * 分页查询
     *
     * @param pageRequest
     * @return
     */
    public Page<Post> postList(PageRequest pageRequest) {
        return dao.findAll(pageRequest);
    }

    /**
     * id查询
     *
     * @param id
     * @return
     */
    public Post findById(Long id) {
        return dao.findById(id).get();
    }

    /**
     * 更新数据
     *
     * @param post
     * @return
     */
    public Post update(Post post) {
        Post dbPost = findById(post.getId());
        BeanUtil.copyProperties(post, dbPost);
        return dao.save(dbPost);
    }
}
